package com.windfone.web.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;
import com.windfone.model.Fitting;
import com.windfone.model.Product;
import com.windfone.model.ProductType;
import com.windfone.service.FittingService;
import com.windfone.service.ProductService;
import com.windfone.service.ProductTypeService;

public class SearchCenterAction extends ActionSupport {
	private ProductService productService;
	private FittingService fittingService;
	private ProductTypeService productTypeService;
	private String typeId  ;
//	private int status ;
	private List<Product> products;
	private List<Fitting> fittings;
	private String beginPrice;
	private String endPrice;
	private String productName;
	private List<ProductType> productTypeList;
	private String searchType;
	
	public List<Fitting> getFittings() {
		return fittings;
	}

	public void setFittings(List<Fitting> fittings) {
		this.fittings = fittings;
	}

	public String getSearchType() {
		return searchType;
	}

	public void setSearchType(String searchType) {
		this.searchType = searchType;
	}

	public List<Product> getProducts() {
		return products;
	}

	public ProductTypeService getProductTypeService() {
		return productTypeService;
	}

	public String getBeginPrice() {
		return beginPrice;
	}

	public String getEndPrice() {
		return endPrice;
	}

	public String getProductName() {
		return productName;
	}

	public void setFittingService(FittingService fittingService) {
		this.fittingService = fittingService;
	}

	public void setBeginPrice(String beginPrice) {
		this.beginPrice = beginPrice;
	}

	public void setEndPrice(String endPrice) {
		this.endPrice = endPrice;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}

	public List<ProductType> getProductTypeList() {
		return productTypeList;
	}

	public void setProductTypeService(ProductTypeService productTypeService) {
		this.productTypeService = productTypeService;
	}

	public void setProductTypeList(List<ProductType> productTypeList) {
		this.productTypeList = productTypeList;
	}

	public void setProducts(List<Product> products) {
		this.products = products;
	}

//	public int getStatus() {
//		return status;
//	}
//
//	public void setStatus(int status) {
//		this.status = status;
//	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	private String headSearchCondition;
	public String getHeadSearchCondition() {
		return headSearchCondition;
	}

	public void setHeadSearchCondition(String headSearchCondition) {
		this.headSearchCondition = headSearchCondition;
	}

	private String dealParam(String para){
		String rtv = "";
		try {
			if ( para != null ){
				byte[] bytes = para.getBytes("ISO8859_1");
				rtv = new String(bytes, "UTF-8");
			}
		 } catch (UnsupportedEncodingException e) {
		    e.printStackTrace();
		 }
		 return rtv;
	}
	@Override
	public String execute() throws Exception {
		productTypeList = productTypeService.listProductType();
		boolean flash = false;
		if ( searchType.equals("1")){
			String whereStr = "  p.productTypeId=t.id AND p.status=1  ";
			if ( headSearchCondition!=null && !headSearchCondition.equals("") && headSearchCondition.indexOf("*") ==-1 &&  headSearchCondition.indexOf("%") ==-1){
				whereStr=whereStr+" AND p.name LIKE '%"+dealParam( headSearchCondition )+"%'";
				flash =true;
			}
			if ( flash )
				products = productService.getAllProductByWherecondition(whereStr );
			else 
				products = new ArrayList<Product>();
		} else if ( searchType.equals("2")){
			String whereStr ="";
			if ( headSearchCondition!=null && !headSearchCondition.equals("") && headSearchCondition.indexOf("*") ==-1 &&  headSearchCondition.indexOf("%") ==-1){
				whereStr = whereStr +" AND typeName LIKE '%"+ dealParam( headSearchCondition ) +"%' " ;
				flash =true;
			}
			if ( flash )
				fittings = fittingService.getAllFittingNolimit( whereStr );
		}
		return SUCCESS;
	}

	
	public String searchForMobile() throws Exception {
		productTypeList = productTypeService.listProductType();
		String whereStr = "  p.productTypeId=t.id AND p.status=1  ";
		boolean flash = false;
		if ( typeId !=null && !"".equals(typeId) && !"-1".equals(typeId) && isInteger( typeId )){
			whereStr=whereStr+" AND p.productTypeId="+typeId;
			flash =true;
		}
		if ( productName!=null && !productName.equals("") && productName.indexOf("*") ==-1 &&  productName.indexOf("%") ==-1){
			whereStr=whereStr+" AND p.name LIKE '%"+productName+"%'";
			flash =true;
		}
		if ( beginPrice!=null && !beginPrice.equals("-1") && isInteger( beginPrice )){
			whereStr=whereStr+" AND p.price >="+beginPrice;
			flash =true;
		}
		if ( endPrice!=null && !endPrice.equals("-1") && isInteger( endPrice )){
			whereStr=whereStr+" AND p.price <="+endPrice;
			flash =true;
		}
		if ( flash )
			products = productService.getAllProductByWherecondition(whereStr );
		else 
			products = new ArrayList<Product>();
		productName = "";
		beginPrice = "";
		endPrice = "";
		typeId= "";
		return SUCCESS;
	}
	
	
	public String getTypeId() {
		return typeId;
	}

	public void setTypeId(String typeId) {
		this.typeId = typeId;
	}
	
	private  boolean isInteger(String value) {
		try {
			new Integer(value);
			return true;
		} catch (NumberFormatException e) {
			return false;
		}
	}
}
